package com.css.fghs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.css.fghs.constants.Constants;
import com.css.fghs.entity.domain.FgjyXqzxXqxxbDO;
import com.css.fghs.entity.dto.XqxxReqDTO;
import com.css.fghs.entity.vo.XqxxVO;
import com.css.fghs.service.FgjyXqzxXqxxbService;
import com.css.fghs.mapper.FgjyXqzxXqxxbMapper;
import com.css.session.SessionUtils;
import com.css.util.GyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 21062
* @description 针对表【fgjy_xqzx_xqxxb(需求信息表)】的数据库操作Service实现
* @createDate 2025-08-27 16:56:40
*/
@Service
@Slf4j
public class FgjyXqzxXqxxbServiceImpl extends ServiceImpl<FgjyXqzxXqxxbMapper, FgjyXqzxXqxxbDO>
    implements FgjyXqzxXqxxbService{

    @Override
    public Page<FgjyXqzxXqxxbDO> getXqxx(XqxxReqDTO reqDTO) {
        log.info("---------------"+ SessionUtils.getYhuuid() + "--------------------------");
        Page<FgjyXqzxXqxxbDO> page = new Page<>(reqDTO.getPageNum(), reqDTO.getPageSize());
        LambdaQueryWrapper<FgjyXqzxXqxxbDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(FgjyXqzxXqxxbDO::getXquuid, FgjyXqzxXqxxbDO::getBt, FgjyXqzxXqxxbDO::getSplx,FgjyXqzxXqxxbDO::getXzqhsf,
                FgjyXqzxXqxxbDO::getXzqhds,FgjyXqzxXqxxbDO::getDj,FgjyXqzxXqxxbDO::getDw,FgjyXqzxXqxxbDO::getSl,FgjyXqzxXqxxbDO::getDqzt,
                FgjyXqzxXqxxbDO::getLrrq,FgjyXqzxXqxxbDO::getXqlx);
        queryWrapper.eq(FgjyXqzxXqxxbDO::getXybz,"Y");
        queryWrapper.eq(FgjyXqzxXqxxbDO::getYxbz,"Y");
        if ("Y".equals(reqDTO.getYhbz())){
            queryWrapper.eq(FgjyXqzxXqxxbDO::getYhuuid,SessionUtils.getYhuuid());
        }
        if (GyUtils.isNotNull(reqDTO.getXqlx())){
            queryWrapper.eq(FgjyXqzxXqxxbDO::getXqlx,reqDTO.getXqlx());
        }
        if (GyUtils.isNotNull(reqDTO.getSplx())) {
            queryWrapper.eq(FgjyXqzxXqxxbDO::getSplx, reqDTO.getSplx());
        }
        if (GyUtils.isNotNull(reqDTO.getXzqhsf())){
            queryWrapper.eq(FgjyXqzxXqxxbDO::getXzqhsf,reqDTO.getXzqhsf());
        }
        if (GyUtils.isNotNull(reqDTO.getXzqhds())){
            queryWrapper.eq(FgjyXqzxXqxxbDO::getXzqhds,reqDTO.getXzqhds());
        }
        if (GyUtils.isNotNull(reqDTO.getXzqhqx())){
            queryWrapper.eq(FgjyXqzxXqxxbDO::getXzqhqx,reqDTO.getXzqhqx());
        }
        if (GyUtils.isNotNull(reqDTO.getBt())){
            queryWrapper.like(FgjyXqzxXqxxbDO::getBt,reqDTO.getBt());
        }
        if (GyUtils.isNotNull(reqDTO.getSpdl())){
            queryWrapper.like(FgjyXqzxXqxxbDO::getSplx,reqDTO.getSpdl());
        }
        if (GyUtils.isNotNull(reqDTO.getSpzl())){
            queryWrapper.like(FgjyXqzxXqxxbDO::getSplx,reqDTO.getSpzl());
        }
        if(GyUtils.isNotNull(reqDTO.getFbrqq())) {
            queryWrapper.ge(FgjyXqzxXqxxbDO::getLrrq,reqDTO.getFbrqq());
        }
        if(GyUtils.isNotNull(reqDTO.getFbrqz())) {
            queryWrapper.le(FgjyXqzxXqxxbDO::getLrrq,reqDTO.getFbrqz());
        }
        return page(page, queryWrapper);
    }

    @Override
    public String saveXqxx(XqxxVO xqxxVO) {
        FgjyXqzxXqxxbDO xqxx = new FgjyXqzxXqxxbDO();
        BeanUtils.copyProperties(xqxxVO, xqxx);
        xqxx.setXquuid(GyUtils.getUuid());
        xqxx.setYhuuid(SessionUtils.getYhuuid());
        xqxx.setYxqq(new Date());
        xqxx.setDqzt(Constants.XQXX_DQZT_DSH);
        // todo 存图片
        xqxx.setTplj(xqxxVO.getTplj());
        xqxx.setXybz("Y");
        xqxx.setYxbz("Y");
        xqxx.setLrrq(new Date());
        xqxx.setLrrsfid(SessionUtils.getYhuuid());
        this.save(xqxx);
        return "success";
    }

    @Override
    public XqxxVO getXqxxxq(String xquuid) {
        XqxxVO xqxxVO = new XqxxVO();
        FgjyXqzxXqxxbDO xqxx = this.getById(xquuid);
        BeanUtils.copyProperties(xqxx, xqxxVO);
        if (GyUtils.isNotNull(xqxxVO.getXqlx()))
        {
            String[] xqlx = xqxxVO.getXqlx().split(",");
            // todo 缺码表
            xqxxVO.setSpdl(xqlx[0]);
            xqxxVO.setSpzl(xqlx[1]);
        }
        return xqxxVO;
    }


    @Override
    public String updateXqzt(XqxxVO xqxxVO) {
        FgjyXqzxXqxxbDO xqxx = new FgjyXqzxXqxxbDO();
        xqxx.setXquuid(xqxxVO.getXquuid());
        xqxx.setDqzt(xqxxVO.getDqzt());
        xqxx.setXgrq(new Date());
        this.updateById(xqxx);
        return "success";
    }
}




